import { director, game, RenderRoot2D } from "cc";
import Singleton from "../Base/Singleton";
import { DEFAULT_DURATION, DrawManger } from "../Scripts/UI/DrawManger";
import { createUINode } from "../Scripts/Utils";


export class FadeManager extends Singleton {

    static get Instance() {
        return super.GetInstance<FadeManager>();
    }
    private _fader: DrawManger = null;

    get fader() {
        if (this._fader !== null) {
            return this._fader;
        }

        const root = createUINode('Root');
        root.addComponent(RenderRoot2D);

        const fadeNode = createUINode('FadeNode', root);
        this._fader = fadeNode.addComponent(DrawManger);
        this._fader.init();

        director.addPersistRootNode(root);

        return this._fader;
    }

    fadeIn(duration: number = DEFAULT_DURATION) {
        return this.fader.fadeIn(duration);
    }

    fadeOut(duration: number = DEFAULT_DURATION) {
        return this.fader.fadeOut(duration);
    }

    mask() {
        return this.fader.mask();
    }
}


